package org.rainbowinverted.charging.device.connect.bl.server.util;


import org.rainbowinverted.charging.device.api.operation.constants.DeviceOpConstantEnum;

import java.util.HashMap;
import java.util.Map;

/**
 * op代码映射
 *
 * @author huaiping
 */
public class BlOpCodeMap {
    private static final Map<Integer, Integer> OP_CODE_MAP = new HashMap<>(8){{
        put(DeviceOpConstantEnum.HEARTBEAT.getOpCode(), 0x1001);
        put(DeviceOpConstantEnum.DEVICE_STATUS_REPORT.getOpCode(), 0x1002);
        put(DeviceOpConstantEnum.DEVICE_CHARGE_FINISH_REPORT.getOpCode(), 0x1004);
        put(DeviceOpConstantEnum.CONTROL_ON_OFF.getOpCode(), 0x1007);
        put(DeviceOpConstantEnum.UPDATE_DEVICE_FTP.getOpCode(), 0x1008);
        put(DeviceOpConstantEnum.QUERY_DEVICE_STATUS.getOpCode(), 0x1003);
        put(DeviceOpConstantEnum.QUERY_GUN_INFO.getOpCode(), 0x1003);
        put(DeviceOpConstantEnum.DEVICE_EVENT_REPORT.getOpCode(), 0x1013);
        put(DeviceOpConstantEnum.UPDATE_DEVICE_QR.getOpCode(), 0x1014);
        put(DeviceOpConstantEnum.REBOOT.getOpCode(), 0x100D);
    }};

    /**
     * 转换opCode
     * 将平台opCode转换成目标设备opCode
     * @param platformOpCode 源op代码
     * @return {@link Integer}
     */
    public static Integer convertOpCodeToDeviceCode(Integer platformOpCode) {
        return OP_CODE_MAP.get(platformOpCode);
    }

    /**
     * 转换opCode
     * 将设备opCode转换成平台opCode
     * @param deviceOpCode 源op代码
     * @return {@link Integer}
     */
    public static Integer convertOpCodeToPlatformCode(Integer deviceOpCode) {
        for (Map.Entry<Integer, Integer> entry : OP_CODE_MAP.entrySet()) {
            if (entry.getValue().equals(deviceOpCode)) {
                return entry.getKey();
            }
        }
        return null;
    }
}
